unit From_Server;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzPanel, StdCtrls, RzEdit, RzButton, Mask, RzBtnEdt,
  rtcInfo, rtcConn, rtcTcpSrv, RzRadChk, rtcDataSrv, rtcMsgSrv;

type
  TFormServer = class(TForm)
    Memo_1: TRzMemo;
    RzToolbar1: TRzToolbar;
    Edit_Serv: TRzButtonEdit;
    Edit_Port: TRzButtonEdit;
    RzCheckBox1: TRzCheckBox;
    RzToolButton1: TRzToolButton;
    TcpServer_A: TRtcTcpServer;
    RtcMessageServer1: TRtcMessageServer;
    procedure RzCheckBox1Click(Sender: TObject);
    procedure TcpServer_AClientConnect(Sender: TRtcConnection);
    procedure TcpServer_ADataReceived(Sender: TRtcConnection);
    procedure TcpServer_AClientDisconnect(Sender: TRtcConnection);
    procedure RzToolButton1Click(Sender: TObject);
    procedure RtcMessageServer1DataIn(Sender: TRtcConnection);
    procedure RtcMessageServer1RequestAccepted(Sender: TRtcConnection);
  private
    FDataReceived:string;
  public
    procedure AddText(AText:string);
  end;

var
  FormServer: TFormServer;

implementation

uses
  rtcLog;

{$R *.dfm}

procedure TFormServer.AddText(AText: string);
begin
  Memo_1.Lines.Add(AText);
end;

procedure TFormServer.RzCheckBox1Click(Sender: TObject);
begin
  TcpServer_A.ServerAddr:=Trim(Edit_Serv.Text);
  TcpServer_A.ServerPort:=Trim(Edit_Port.Text);
  if TRzCheckBox(Sender).Checked then
  begin
    TcpServer_A.Listen();
  end else
  begin
    TcpServer_A.StopListen;
  end;

  {RtcMessageServer1.ServerAddr:=Trim(Edit_Serv.Text);
  RtcMessageServer1.ServerPort:=Trim(Edit_Port.Text);
  if TRzCheckBox(Sender).Checked then
  begin
    RtcMessageServer1.Listen();
  end else
  begin
    RtcMessageServer1.StopListen;
  end;}
end;

procedure TFormServer.TcpServer_AClientConnect(Sender: TRtcConnection);
begin
  AddText('some one was connected');
end;

procedure TFormServer.TcpServer_ADataReceived(Sender: TRtcConnection);
var
  NumbA:Integer;
  TextA:string;
begin
  TextA:=Sender.Read;

  AddText(TextA);
  AddText('-------------------------------------------------------------------');
  AddText(Format('%D:%S',[Length(TextA),TextA]));
  //Log(TextA,'receive.txt');
  FDataReceived:=FDataReceived+TextA;

  NumbA:=StrToIntDef(Copy(FDataReceived,1,6),0);

  if NumbA+30=Sender.ReadCount then
  begin
    Log(FDataReceived,'receive.txt');
    Sender.Disconnect;
  end;  
end;

procedure TFormServer.TcpServer_AClientDisconnect(Sender: TRtcConnection);
begin
  AddText('some one was disconnected');
  FDataReceived:='';
end;

procedure TFormServer.RzToolButton1Click(Sender: TObject);
begin
  Memo_1.Lines.Clear;
end;

procedure TFormServer.RtcMessageServer1DataIn(Sender: TRtcConnection);
begin
  Memo_1.Lines.Add(Sender.Read);
end;

procedure TFormServer.RtcMessageServer1RequestAccepted(
  Sender: TRtcConnection);
begin
  Memo_1.Lines.Add(Sender.Read);
end;

initialization;
begin
  StartLog;
end;

finalization
begin

end;    
end.
